﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.CiZhiTuiXiuServiceReference;
namespace CommunitySystem
{
    public partial class TuiXiuWindow : ChildWindow
    {
        public int OrgID = 0;
        public int ID = 0;
        public bool IsNew = false;
        //public ObservableCollection<CiZhiModel> CiZhiList = new ObservableCollection<CiZhiModel>();
        public TuiXiuModel model = new TuiXiuModel();
        private CiZhiTuiXiuServiceClient _Client = new CiZhiTuiXiuServiceClient();
        public TuiXiuWindow(int orgid, int id, bool isnew)
        {
            InitializeComponent();
            OrgID = orgid;
            IsNew = isnew;
            ID = id;
            _Client.GetTuiXiuModelCompleted += new EventHandler<GetTuiXiuModelCompletedEventArgs>(_Client_GetTuiXiuModelCompleted);
            _Client.GetTuiXiuModelAsync("0", ID);
            _Client.InsertTuiXiuCompleted += new EventHandler<InsertTuiXiuCompletedEventArgs>(_Client_InsertTuiXiuCompleted);
            _Client.UpdateTuiXiuCompleted += new EventHandler<UpdateTuiXiuCompletedEventArgs>(_Client_UpdateTuiXiuCompleted);
        }

        void _Client_UpdateTuiXiuCompleted(object sender, UpdateTuiXiuCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
            this.DialogResult = true;
        }

        void _Client_InsertTuiXiuCompleted(object sender, InsertTuiXiuCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
            this.DialogResult = true;
        }

        void _Client_GetTuiXiuModelCompleted(object sender, GetTuiXiuModelCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                model = e.Result[0];
                names.Text = model.Name;
                this.DataContext = model;
            }
            else
            {
                model = new TuiXiuModel();
                this.DataContext = model;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsNew)
            {
                model.OrgID = OrgID;
                _Client.InsertTuiXiuAsync(model);
            }
            else
            {
                _Client.UpdateTuiXiuAsync(model);
            }
        }

        private void OKButton_Click1(object sender, RoutedEventArgs e)
        {
            AddCiZhiTuiXiuEmpWindow RMWindow;
            if (IsNew)
                RMWindow = new AddCiZhiTuiXiuEmpWindow(OrgID.ToString());
            else
            {
                RMWindow = new AddCiZhiTuiXiuEmpWindow(model.OrgID.ToString());
            }
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }

        void RMWindow_Closed(object sender, EventArgs e)
        {
            if (((AddCiZhiTuiXiuEmpWindow)sender).DialogResult == true)
            {
                names.Text = ((AddCiZhiTuiXiuEmpWindow)sender).EmpName;
                model.Name = ((AddCiZhiTuiXiuEmpWindow)sender).EmpName;
                model.EmpbasicID = ((AddCiZhiTuiXiuEmpWindow)sender).EmpbasicID[0];
            }

            //foreach(int i in ((AddCiZhiTuiXiuEmpWindow)sender).EmpbasicID)
            //{
            //    CiZhiModel c = new CiZhiModel() {EmpbasicID = i};
            //}
        }
    }
}
